"""
#
# Flow stability for dynamic community detection https://arxiv.org/abs/2101.06131v2
#
# Copyright (C) 2021 Alexandre Bovet <alexandre.bovet@maths.ox.ac.uk>
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation; either version 3 of the License, or (at your option) any
# later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.


Creates the temporal network (ContTempNetwork) using the raw data for the
mice dataset and saves it as `micenet_2017_02_28_to_2017_05_01.pickle`


"""

import sys
import os
PACKAGE_PARENT = '..'
SCRIPT_DIR = os.path.dirname(os.path.realpath(os.path.join(os.getcwd(), os.path.expanduser(__file__))))
sys.path.append(os.path.normpath(os.path.join(SCRIPT_DIR, PACKAGE_PARENT)))

import json

import numpy as np
import pandas as pd
from TemporalNetwork import ContTempNetwork

from datetime import  datetime

datadir = '../paper_data/mice_data_march_april2017/'


net_file = os.path.join(datadir,'micenet_2017_02_28_to_2017_05_01')


#%% this is the data available at https://zenodo.org/record/4725155 (doi:10.5281/zenodo.4725155)
#t=0 corresponds to Feb. 28 2017 00:00:00

contact_sequence = pd.read_csv('../data/mice_contact_sequence.csv')


with open('../data/mice_sex.json','r') as fopen:
    sex_dict = json.load(fopen)    

# create continuous time network instance
net = ContTempNetwork(events_table=contact_sequence)

#%%

net._compute_time_grid()

net.male_array = np.isin(net.node_label_array, sex_dict['male'])
net.female_array = np.isin(net.node_label_array, sex_dict['female'])

net.start_date = datetime(2017,2,28)

net.save(net_file, attributes_list=['node_to_label_dict',
                      'events_table',
                      'times',
                      'time_grid',
                      'num_nodes',
                      'start_date',
                      'male_array',
                      'female_array'])
